package com.kltn.zigo.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.kltn.zigo.process.JSONParser;

public class UserInfoData {
	JSONParser jsonParser = new JSONParser();
    
    // url to inser user
    private static String url_insert_user = IP.get_IP() + IP.get_path_service() + "insert_user.php";
	private static String url_update_user = IP.get_IP() +  IP.get_path_service() + "update_user.php";
	private static String url_get_user = IP.get_IP() +  IP.get_path_service() + "get_user.php";

    JSONArray users = null;
	
    private static final String TAG_SUCCESS = "success";
    
    public final String TAG_SUCCESS_GET_INFO = "success";
    public final String TAG_USER = "user";
    public final String TAG_USER_ID = "user_id";
    public final String TAG_USER_ID_ZING = "user_id_zing";
    public final String TAG_USER_NAME_ZING = "user_name_zing";
    public final String TAG_USER_DISPLAY_NAME = "user_display_name";
    public final String TAG_USER_AVATAR_ZING = "user_avatar_zing";
    

    String user_id_zing;
    String user_name_zing;
    String user_avatar_zing;
    String user_display_name;
    
    public UserInfoData(String UserIdZing, String UserNameZing, String UserDisplayName, String UserAvatarZing)
    {
    	user_id_zing = UserIdZing;
    	user_name_zing = UserNameZing;
    	user_display_name = UserDisplayName;
    	user_avatar_zing = UserAvatarZing;
    }
    
    public HashMap<String, String> GetUser()
    {
    	ConfirmDataUser();
    	
    	HashMap<String, String> usersList = new HashMap<String,String>();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("user_id_zing", user_id_zing));
        // getting JSON string from URL
        JSONObject json = jsonParser.makeHttpRequest(url_get_user, "GET", params);

        Log.d("All user: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
            	users = json.getJSONArray(TAG_USER);
            	
	                // looping through All Products
                for (int i = 0; i < users.length(); i++) {
                    JSONObject c = users.getJSONObject(i);

                    // Storing each json item in variable
                    String userIdZing = user_id_zing;
                    String userNameZing = c.getString(TAG_USER_NAME_ZING);
                    String userDisplayName = c.getString(TAG_USER_DISPLAY_NAME);
                    String userAvatarZing = c.getString(TAG_USER_AVATAR_ZING);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_USER_ID_ZING, userIdZing);
                    map.put(TAG_USER_NAME_ZING, userNameZing);
                    map.put(TAG_USER_DISPLAY_NAME, userDisplayName);
                    map.put(TAG_USER_AVATAR_ZING, userAvatarZing);

                    // adding HashList to ArrayList
                    usersList = map;
               }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return usersList;
    }
    
    
    
    
    public void InsertUser()
	{
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("user_id_zing", user_id_zing));
        params.add(new BasicNameValuePair("user_name_zing", user_name_zing));
        params.add(new BasicNameValuePair("user_display_name", user_display_name));
        params.add(new BasicNameValuePair("user_avatar_zing", user_avatar_zing));

        // getting JSON Object
        // Note that create place url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_insert_user, "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created placex
                //Intent i = new Intent(activity.getApplicationContext(), ListPlaceActivity.class);
                //activity.startActivity(i);

                // closing this screen
                //activity.finish();
                
            } else {
                // failed to create place
            	
            	
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
	}
    
    public void UpdateUser()
	{
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("user_id_zing", user_id_zing));
        params.add(new BasicNameValuePair("user_name_zing", user_name_zing));
        params.add(new BasicNameValuePair("user_display_name", user_display_name));
        params.add(new BasicNameValuePair("user_avatar_zing", user_avatar_zing));

        // getting JSON Object
        // Note that create place url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_update_user, "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created placex
                //Intent i = new Intent(activity.getApplicationContext(), ListPlaceActivity.class);
                //activity.startActivity(i);

                // closing this screen
                //activity.finish();
                
            } else {
                // failed to create place
            	
            	
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
	}
    
    public void ConfirmDataUser()
    {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("user_id_zing", user_id_zing));
        // getting JSON string from URL
        JSONObject json = jsonParser.makeHttpRequest(url_get_user, "GET", params);

        Log.d("All user: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) 
            {           	
            	UpdateUser();
            } 
            else
            {
            	InsertUser();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
